//
//  InSoundTool.m
//  Inner  Voice
//
//  Created by 徐海宇 on 16/2/29.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InSoundTool.h"

#import <FMDB.h>

#import "InSound.h"

@implementation InSoundTool

static FMDatabase *_dataBase = nil;

+ (NSString *)sadBox{
    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"sounds.sqlite"];
}

+ (FMDatabase *)shareDataBase {
    if (_dataBase == nil) {
        NSString *path = [self sadBox];
        
        NSLog(@"播放器fmdb %@",path);
        _dataBase = [FMDatabase databaseWithPath:path];
        
        if (_dataBase.open) {
            NSLog(@"播放器fmdb能打开,创建成功");
            
            
            NSString *sql = @"CREATE TABLE IF NOT EXISTS sounds (inSound_id INTEGER PRIMARY KEY AUTOINCREMENT,inSound_name TEXT,inSound_icon TEXT,inSound_Url TEXT,sound_id TEXT);";
            
            BOOL success = [_dataBase executeUpdate:sql];
            if (success) {
                NSLog(@"播放器fmdb创建表成功");
            }else {
                NSLog(@"播放器fmdb创建表失败");
                NSLog(@"播放器fmdb %@",_dataBase.lastErrorMessage);
            }
            
        }else {
            NSLog(@"播放器fmdb 创建数据库失败");
        }
    }
    return _dataBase;
}


+ (NSMutableArray *)soundArray {
    NSMutableArray *soundArray = [NSMutableArray array];
    FMResultSet * result = [[self shareDataBase]executeQuery:@"SELECT * FROM sounds"];
    
    while (result.next)
    {
        InSound *inSound = [[InSound alloc]init];
        inSound.inSound_id = (NSInteger)[result intForColumn:@"inSound_id"];
        inSound.inSound_name = [result stringForColumn:@"inSound_name"];
        inSound.inSound_icon = [result stringForColumn:@"inSound_icon"];
        inSound.inSound_Url = [result stringForColumn:@"inSound_Url"];
        inSound.sound_id = [result stringForColumn:@"sound_id"];
        
        [soundArray addObject:inSound];
    }
    return soundArray;
}


+ (BOOL)insertInToSound:(InSound *)sound {
    
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM sounds WHERE sound_id = ?",sound.sound_id];
    
    if(result.next) {
        NSLog(@"播放器fmdb已经有此数据");
        return  NO;
    }else {
        BOOL success = [[self shareDataBase]executeUpdate:@"INSERT INTO sounds (inSound_name,inSound_icon,inSound_Url,sound_id) VALUES (?, ?,?,?)",sound.inSound_name,sound.inSound_icon,sound.inSound_Url,sound.sound_id];

    if (success) {
        NSLog(@"播放器fmdb插入成功");
    }else {
        NSLog(@"播放器fmdb插入失败");
        NSLog(@"播放器fmdb%@",[self shareDataBase].lastErrorMessage);
    
    }
        return success;
    }
}


+ (BOOL)deleteSound:(InSound *)sound {
    
    FMResultSet *result = [[self shareDataBase]executeQuery:@"SELECT * FROM sounds WHERE sound_id = ?",sound.sound_id];
    
    if(!result.next) {
        NSLog(@"播放器fmdb无此数据");
        return  NO;
    }else {
        BOOL success = [[self shareDataBase]executeUpdate:@"DELETE FROM sounds WHERE sound_id = ?",sound.sound_id];
        if (success) {
            NSLog(@"播放器fmdb删除成功");
        }else {
            NSLog(@"播放器fmdb删除失败");
            NSLog(@"播放器fmdb%@",[self shareDataBase].lastErrorMessage);
            
        }
        return success;
    }

}


+ (void)closeDataBase {
    [[self shareDataBase]close];
}

@end
